//
//  NChangePWDViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/9.
//  Copyright © 2018 youedata. All rights reserved.
//

#import "NChangePWDViewController.h"

@interface NChangePWDViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *oldPWD;


@property (weak, nonatomic) IBOutlet UITextField *freshPWD;


@property (weak, nonatomic) IBOutlet UITextField *confirmPWD;


@end

@implementation NChangePWDViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.navigationItem setItemWithTitle:@"修改密码" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 40, 40) title:@"确定" Image:nil selectedIMG:nil tartget:self action:@selector(changeAction)];
    self.freshPWD.delegate = self;
    self.confirmPWD.delegate = self;
    UIView *lineone = [[UIView alloc]initWithFrame:CGRectMake(0, 62, ScreenWidth, 0.5)];
    UIView *linetwo = [[UIView alloc]initWithFrame:CGRectMake(0, 120, ScreenWidth, 0.5)];
    UIView *linethree = [[UIView alloc]initWithFrame:CGRectMake(0, 178, ScreenWidth, 0.5)];
    lineone.backgroundColor = kLightGrayColor;
    linethree.backgroundColor = kLightGrayColor;
    linetwo.backgroundColor = kLightGrayColor;
    [self.view addSubview:lineone];
    [self.view addSubview:linetwo];
    [self.view addSubview:linethree];
    // Do any additional setup after loading the view from its nib.
}


///修改动作
- (void)changeAction {
    
    //密码写完要存起来
    if (!self.oldPWD.text.length||!self.freshPWD.text||!self.confirmPWD.text) {
        [AFToast showText:@"请完善信息"];
        return;
    }
    //提交时判断密码是否够位数
    if (self.freshPWD.text.length<6||self.confirmPWD.text.length<6) {
        [AFToast showText:@"请输入6-20位的密码"];
        return;
    }
    
    //下次修改时候可以拿到
    [[NSUserDefaults standardUserDefaults] setValue:@"pwd" forKey:self.freshPWD.text];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
    
    NSDictionary *dic = @{@"userInfoId":[NUserModel shareInstance].userInfoId,@"userInfoNamePwd":self.oldPWD.text,@"userInfoNamePwdNew":self.freshPWD.text,@"userInfoNamePwdReword":self.confirmPWD.text};
    [[NNRCNetRequest manager]getWithUrl:kChangePWDUrl params:dic success:^(id result) {
        
        YYLog(@"%@",result);
        if ([result[@"code"] integerValue]==0) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 [self.navigationController popViewControllerAnimated:YES];
            });
            [AFToast showText:result[@"修改成功"]];
        }else{
            
            [AFToast showText:result[@"msg"]];

        }
       
        
    } failure:^(id failure) {
        
        YYLog(@"%@",failure);
        
    }];
    
}



#pragma mark - textfield delegate  限制标题输入长度
//- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//
//    if (self.freshPWD == textField) {
//        if (textField.text.length<6) {
//            return NO;
//        }
//    }
//    return YES;
//}


//- (void)textFieldDidEndEditing:(UITextField *)textField
//{
//    if (self.freshPWD == textField) {
//        if (textField.text.length<6) {
//            [AFToast showText:@"字符长度不能少于6位"];
//        }
//    }
//}



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.freshPWD) {
        
        //这里的if是为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.freshPWD.text.length >= 20) {
            self.freshPWD.text = [textField.text substringToIndex:20];
            return NO;
        }
        
    }
    
    if (textField == self.confirmPWD) {
        
        //这里的if是为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.confirmPWD.text.length >= 20) {
            self.confirmPWD.text = [textField.text substringToIndex:20];
            return NO;
        }
        
    }
    
    return YES;
    
}


@end
